package com.xlh.virtualization.dao.helper;

import com.xlh.virtualization.dao.StatisticMapper;
import com.xlh.virtualization.domain.Statistic;
import com.xlh.virtualization.domain.example.StatisticExample;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author cheer
 */
@Repository
public class StatisticMapperHelper {

    @Autowired
    private StatisticMapper statisticMapper;

    public Statistic getByUserIdAndContext(Long userId, Integer contextType, Long contextId) {
        StatisticExample example = new StatisticExample();
        example.or().andUserIdEqualTo(userId)
                .andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andDeletedEqualTo(false);
        List<Statistic> statistics = statisticMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(statistics)) {
            return statistics.get(0);
        }
        return null;
    }

    public List<Statistic> listByUserId(Long userId){
        StatisticExample example = new StatisticExample();
        example.or().andUserIdEqualTo(userId)
                .andDeletedEqualTo(false);
        return statisticMapper.selectByExample(example);
    }

    public List<Statistic> getByUserIdsAndContext(List<Long> userIds, Integer contextType, Long contextId) {
        StatisticExample example = new StatisticExample();
        example.or().andUserIdIn(userIds)
                .andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andDeletedEqualTo(false);
        List<Statistic> statistics = statisticMapper.selectByExample(example);

        return statistics;
    }


}
